###trans to .doc
import wx
import os
from io import StringIO
from io import open
from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter, process_pdf
from pdfminer.layout import LAParams
from pdfminer.converter import PDFPageAggregator
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFTextExtractionNotAllowed
from pdfminer.layout import *
#from docx import Document as document
#document = Document()


app = wx.App()
window = wx.Frame(None,title='test',size=(600,700))
panel = wx.Panel(window)

font = wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False)
font1 = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False)


class FileDropTarget(wx.FileDropTarget):
    def __init__(self, window):
        wx.FileDropTarget.__init__(self)
        self.window = window

    def OnDropFiles(self, x, y, filePath):
        #self.window.SetValue(fileNames)
        path = filePath[0]
        self.window.SetValue(path)
        #print(path)
        openFile1(path)


def openFile(event):
    file_dlg = wx.FileDialog(None,message=u"选择文件",defaultDir=os.getcwd(),defaultFile="",style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
    if file_dlg.ShowModal() == wx.ID_OK:
        print(file_dlg.GetPath())
    filename.SetValue(file_dlg.GetPath())
    print(filename.GetValue())
    openFile1(file_dlg.GetPath())

def openFile1(filename_get):

    pdfFile = open(filename_get, "rb")
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)

    process_pdf(rsrcmgr, device, pdfFile)
    device.close()

    content_pdf = retstr.getvalue()
    retstr.close()
    #print(content_pdf)
    content.SetValue(content_pdf)

def outFile(event):
    file_out = filename1.GetValue()
    file_in = filename.GetValue()
    print(file_out)
    print(file_in)

    fp = open(file_in, 'rb')  # 以二进制读模式打开
    # 用文件对象来创建一个pdf文档分析器
    parser = PDFParser(fp)
    # 创建一个PDF文档
    doc = PDFDocument()
    # 连接分析器 与文档对象
    parser.set_document(doc)
    doc.set_parser(parser)

    # 提供初始化密码
    # 如果没有密码 就创建一个空的字符串
    doc.initialize()

    # 检测文档是否提供txt转换，不提供就忽略
    if not doc.is_extractable:
        raise PDFTextExtractionNotAllowed
    else:
        # 创建PDf 资源管理器 来管理共享资源
        rsrcmgr = PDFResourceManager()
        # 创建一个PDF设备对象
        laparams = LAParams()
        device = PDFPageAggregator(rsrcmgr, laparams=laparams)
        # 创建一个PDF解释器对象
        interpreter = PDFPageInterpreter(rsrcmgr, device)

        # 用来计数页面，图片，曲线，figure，水平文本框等对象的数量
        num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0

        # 循环遍历列表，每次处理一个page的内容
        for page in doc.get_pages():  # doc.get_pages() 获取page列表
            num_page += 1  # 页面增一
            interpreter.process_page(page)
            # 接受该页面的LTPage对象
            layout = device.get_result()
            for x in layout:
                if isinstance(x, LTImage):  # 图片对象
                    num_image += 1
                if isinstance(x, LTCurve):  # 曲线对象
                    num_curve += 1
                if isinstance(x, LTFigure):  # figure对象
                    num_figure += 1
                if isinstance(x, LTTextBoxHorizontal):  # 获取文本内容
                    num_TextBoxHorizontal += 1  # 水平文本框对象增一
                    # 保存文本内容
                    with open(file_out, 'a', encoding='utf-8') as f:  # 生成doc文件的文件名及路径
                        results = x.get_text()
                        f.write(results)
                        f.write('\n')
        print('对象数量：\n', '页面数：%s\n' % num_page, '图片数：%s\n' % num_image, '曲线数：%s\n' % num_curve, '水平文本框：%s\n'
              % num_TextBoxHorizontal)

##使用相对坐标
openButton = wx.Button(panel,label='open file')
openButton.SetFont(font)
openButton.Bind(wx.EVT_BUTTON,openFile)

filename = wx.TextCtrl(panel,value='input file',style = wx.TE_READONLY)
filename.SetFont(font1)

content = wx.TextCtrl(panel,value = '大伟哥最帅',style=wx.TE_MULTILINE | wx.HSCROLL)
content.SetFont(font1)
content.SetFont(font1)

filename1 = wx.TextCtrl(panel,value='output file')
filename1.SetFont(font1)

outputButton = wx.Button(panel,label='trans file')
outputButton.SetFont(font)
outputButton.Bind(wx.EVT_BUTTON,outFile)

hbox = wx.BoxSizer()
hbox.Add(filename,proportion=1, flag=wx.EXPAND)
hbox.Add(openButton,proportion=0,flag=wx.LEFT, border=5)

hbox1 = wx.BoxSizer()
hbox1.Add(filename1,proportion=1, flag=wx.EXPAND)
hbox1.Add(outputButton,proportion=0,flag=wx.LEFT, border=5)

vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox,proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(content, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5)
vbox.Add(hbox1,proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

panel.SetSizer(vbox)

dropTarget = FileDropTarget(filename)
panel.SetDropTarget(dropTarget)

window.Show()
app.MainLoop()

